package com.zaofan.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by chenyan on 16/5/26.
 */
public class JedisUtils {

    private static final Logger logger = LoggerFactory.getLogger(JedisUtils.class);

    private static String HOST;
    private static int PORT;
    private static String AUTH;
    private static int TIMEOUT;
    private static String ENV;
    private static JedisPool POOL;

    static {
        Properties properties = new Properties();
        File envPropertiesFile = new File(JedisUtils.class.getResource("/env.properties").getPath());
        File jedisPropertiesFile = new File(JedisUtils.class.getResource("/Jedis.properties").getPath());
        try {
            properties.load(new FileInputStream(envPropertiesFile));
            properties.load(new FileInputStream(jedisPropertiesFile));
        } catch (IOException e) {
            logger.error("jedisUtils 加载 env.properties/Jedis.properties 发生异常");
            throw new RuntimeException("jedisUtils 加载 env.properties/Jedis.properties 发生异常");
        }
        HOST = properties.getProperty("jedis.host");
        PORT = Integer.valueOf(properties.getProperty("jedis.port"));
        AUTH = properties.getProperty("jedis.auth");
        TIMEOUT = Integer.valueOf(properties.getProperty("jedis.timeout"));
        ENV = properties.getProperty("env");
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        POOL = new JedisPool(poolConfig, HOST, PORT, TIMEOUT, AUTH);
    }

    private JedisUtils() {

    }

    public static String setex(String key, int seconds, String value) {
        key = ENV + "_" + key;
        Jedis jedis = POOL.getResource();
        String result = jedis.setex(key, seconds, value);
        logger.info("jedis set key:{}, seconds:{}, value:{}, result:{}", key, seconds, value, result);
        jedis.close();
        return result;
    }

    public static String set(String key, String value) {
        key = ENV + "_" + key;
        Jedis jedis = POOL.getResource();
        String result = jedis.set(key, value);
        logger.info("jedis set key:{}, value:{}, result:{}", key, value, result);
        jedis.close();
        return result;
    }

    public static Long inrc(String key) {
        key = ENV + "_" + key;
        Jedis jedis = POOL.getResource();
        Long result = jedis.incr(key);
        logger.info("jedis inrc key:{}, result:{}", key, result);
        jedis.close();
        return result;
    }

    public static Long inrcBy(String key, int value) {
        key = ENV + "_" + key;
        Jedis jedis = POOL.getResource();
        Long result = jedis.incrBy(key, value);
        logger.info("jedis inrcBy key:{}, value:{}, result:{}", key, value, result);
        jedis.close();
        return result;
    }

    /**
     * Get the value of the specified key. If the key does not exist null is returned. If the value
     * stored at key is not a string an error is returned because GET can only handle string values.
     * <p>
     * Time complexity: O(1)
     *
     * @param key
     * @return Bulk reply
     */
    public static String get(String key) {
        key = ENV + "_" + key;
        Jedis jedis = POOL.getResource();
        String result = jedis.get(key);
        logger.info("jedis get key: {}, result:{}", key, result);
        jedis.close();
        return result;
    }

    public static Long del(String key) {
        key = ENV + "_" + key;
        Jedis jedis = POOL.getResource();
        Long result = jedis.del(key);
        logger.info("jedis del key: {}, result:{}", key, result);
        jedis.close();
        return result;
    }

}
